package edu.msse.signup.ui;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mike Calvo@citronellasoftware.com
 *         Created Mar 15, 2008 - 4:17:25 PM
 */
public class SecurityFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = ((HttpServletRequest)servletRequest);
        HttpSession session = request.getSession();
        loginHandler loginHandler = (loginHandler) session.getAttribute("loginHandler");
        if (loginHandler == null || !loginHandler.isLoggedIn()) {
            ((HttpServletResponse)servletResponse).sendRedirect("../login.jsf");
        } else
            filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {
    }
}
